package linkNode;

public class HasCycle {
    public static void main(String[] args) {

        ListNode head = new ListNode(3);
        ListNode next = new ListNode(2);
        head.next = next;
        head.next.next = new ListNode(0);
        head.next.next.next = new ListNode(-4);
        head.next.next.next.next = next;
        System.out.println(new HasCycle().hasCycle(head));
    }

    /**
     * TODO
     * 环形链表: 快慢指针
     * @param head
     * @return
     */
    private boolean hasCycle(ListNode head) {
        if (head == null&& head.next == null)
            return false;
        ListNode slow = head;
        ListNode fast = head.next;
        //TODO：为了防止死循环，fast.next.next 空指针异常
        while (fast != null && fast.next != null&&fast.next.next != null ){
            if (fast == slow)
                return true;
            slow = slow.next;
            fast = fast.next.next;
        }
        return false;
    }


}
